package com.atguigu.mybatisplus;

import cn.hutool.core.date.DateUtil;
import com.atguigu.mybatisplus.entity.User;
import com.atguigu.mybatisplus.mapper.UserMapper;
import com.atguigu.mybatisplus.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author MI
 * @ClassName DateCompare.java
 * @createTime 2021年10月26日 10:45:00
 */
@SpringBootTest
public class DateCompare {
    @Resource
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Test
    public void compareDate() {

        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        LocalDateTime parse = LocalDateTime.parse("2021-10-22 09:48:19", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // 这里直接传递字符串也行,还是传递时间格式吧
        wrapper.ge(User::getUpdateTime, parse);
        List<User> userList = userMapper.selectList(wrapper);
        userList.stream().forEach(vo -> {

            System.out.println(vo);
        });

    }

    @Test
    public void compareLocalDate() {

        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        // 这里直接传递字符串也行,还是传递时间格式吧
        wrapper.le(User::getUpdateTime, LocalDate.parse("2021-08-24").plusDays(1));
        List<User> userList = userMapper.selectList(wrapper);
        userList.stream().forEach(vo -> {
            System.out.println(vo);
        });
    }

    @Test
    public void cal() {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        List<User> userList = userMapper.selectList(wrapper);

        List<CompareUser> collect = userList.stream().map(vo -> {
            CompareUser compareUser = new CompareUser();
            BeanUtils.copyProperties(vo, compareUser);
            compareUser.setTimeDate(DateUtil.formatDate(vo.getUpdateTime()));
            compareUser.setTimeMonth(DateUtil.format(vo.getUpdateTime(), "yyyy-MM"));
            return compareUser;
        }).collect(Collectors.toList());

        Map<String, Long> collect1 = collect.stream().filter(vo -> {
                    boolean equals = StringUtils.equals("2021-08-24", vo.getTimeDate());
                    System.out.println(equals);
                    return equals;
                })
                .collect(Collectors.groupingBy(CompareUser::getTimeDate, Collectors.counting()));
        System.out.println(collect1);

    }

    @Data
    static class CompareUser {
        private Date updateTime;
        private String timeDate;
        /**
         * 月份(2022-01)
         */
        private String timeMonth;
    }
}
